<template>
    <el-dialog :title="$t('msg.添加朋友')" :visible="visible" :modal="true" :width="width" @close="closeDialog"
        append-to-body>
        <div @click="popover_show = false">
            <div class="mb-20">
                <el-input :placeholder="$t('placeholder.请输入账号进行搜索')" prefix-icon="el-icon-search" style="width: 300px"
                    @keyup.enter.native="handleChange" v-model="keywords">
                    <el-button slot="append" icon="el-icon-search" @click="handleChange"></el-button>
                </el-input>
            </div>
            <div class="dialog-main">
                <div class="member-list">
                    <!-- @click="$user(member.user_id)" -->
                    <div v-for="member in list" :key="member.user_id" class="member-item">
                        <div class="member-avatar">
                            <img :src="member.avatar" alt="avatar">
                        </div>
                        <div class="member-content">
                            <span class="member-name">{{ member.realname }}</span>
                            <span class="member-account">{{ member.account }}</span>
                        </div>
                        <div class="member-actions">
                            <el-popover placement="right" trigger="manual" v-model="popover_show">
                                <NotFriend :member="member" @ok="ok_dialog" @close="closeDialog" />
                                <el-button type="primary" size="mini" slot="reference" @click.stop="look(member)">
                                    {{ $t('msg.查看') }}
                                </el-button>
                            </el-popover>
                        </div>
                    </div>
                </div>
                <el-empty v-if="list.length == 0"></el-empty>
            </div>
        </div>
    </el-dialog>
</template>
<script>
import NotFriend from "@/components/message/user/NotFriend.vue";
export default {
    name: 'FriendAdd',
    components: {
        NotFriend
    },
    props: {
        visible: {
            type: Boolean,
            default: false,
        },
        width: {
            type: String,
            default: '612px',
        },
    },
    data() {
        return {
            keywords: "",
            list: [],

            popover_show: false,
            member: {}
        };
    },
    methods: {
        look(item) {
            this.popover_show = true;
            this.member = item;
        },
        ok_dialog(contact) {
            this.closeDialog()
            this.$emit('ok', contact);
        },
        closeDialog() {
            this.popover_show = false;
            this.$emit("update:visible", false);
            this.list = [];
            this.keywords = "";
        },
        async handleChange() {
            if (!this.keywords) return;
            this.$api.imApi.searchUser({ keywords: this.keywords }).then(res => {
                let list = res.data;
                this.list = list;
                // this.list = list.filter(item => !item.friend)
            })
        }
    },
};
</script>
<style lang="scss" scoped>
.dialog-main {
    height: 400px;
    overflow-y: auto;
}

.member-list {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 20px 0 20px 20px;

    .member-item {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        padding: 8px 10px;
        width: 512px;
        border: solid 1px #eee;
        border-radius: 8px;
        margin-bottom: 20px;

        &:last-child {
            margin-right: 0;
        }

        .member-avatar {
            margin-right: 10px;

            img {
                width: 44px;
                height: 44px;
                border-radius: 50%;
            }
        }

        .member-content {
            flex-grow: 1;
            height: 100%;
            padding: 5px 0;
            box-sizing: border-box;
            display: flex;
            flex-direction: column;
            justify-content: space-between;

            .member-name {
                font-weight: bold;
                margin-right: 10px;
            }

            .member-account {
                margin-top: 3px;
                font-size: 12px;
                color: #999;
            }
        }

        .member-actions {
            display: flex;
            justify-content: flex-end;
        }
    }

}
</style>